package com.px.auditRecord.service.impl;

import java.util.List;

import cn.hutool.core.convert.Convert;
import com.px.auditRecord.domain.*;
import com.px.auditRecord.mapper.*;
import com.px.common.utils.DateUtils;
import com.px.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.px.auditRecord.service.IAuditRecordRoutesService;

/**
 * 航线审核表Service业务层处理
 * 
 * @author WRJ
 * @date 2024-08
 */
@Service
public class AuditRecordRoutesServiceImpl implements IAuditRecordRoutesService 
{
    @Autowired
    private AuditRecordRoutesMapper auditRecordRoutesMapper;

    @Autowired
    private RoutesBasicMapper routesBasicMapper;

    @Autowired
    private RoutesSurfaceMapper routesSurfaceMapper;

    @Autowired
    private RoutesSurfacePointMapper routesSurfacePointMapper;

    @Autowired
    private AuditRecordRoutesSurfaceMapper auditRecordRoutesSurfaceMapper;

    @Autowired
    private AuditRecordRoutesSurfacePointMapper auditRecordRoutesSurfacePointMapper;

    @Autowired
    private AuditRecordRoutesDroneMapper auditRecordRoutesDroneMapper;

    @Autowired
    private ForRecordUserMapper forRecordUserMapper;

    @Autowired
    private ForRecordRoutesMapper forRecordRoutesMapper;

    @Autowired
    private ForRecordRoutesDroneMapper forRecordRoutesDroneMapper;

    @Autowired
    private ForRecordRoutesSurfaceMapper forRecordRoutesSurfaceMapper;

    @Autowired
    private ForRecordRoutesSurfacePointMapper forRecordRoutesSurfacePointMapper;



    /**
     * 查询航线审核表
     * 
     * @param id 航线审核表主键
     * @return 航线审核表
     */
    @Override
    public AuditRecordRoutes selectAuditRecordRoutesById(Integer id)
    {
        AuditRecordRoutes auditRecordRoutes = auditRecordRoutesMapper.selectAuditRecordRoutesById(id);
        List<AuditRecordRoutesSurface> auditRecordRoutesSurfaceList = auditRecordRoutesSurfaceMapper.selectAuditRecordRoutesSurfaceByArrId(id);

        auditRecordRoutesSurfaceList.forEach(auditRecordRoutesSurface -> {
            List<AuditRecordRoutesSurfacePoint> auditRecordRoutesSurfacePointList = auditRecordRoutesSurfacePointMapper.selectAuditRecordRoutesSurfacePointListByRoutesSurfaceId(auditRecordRoutesSurface.getId());
            auditRecordRoutesSurface.setAuditRecordRoutesSurfacePointList(auditRecordRoutesSurfacePointList);
        });
        auditRecordRoutes.setAuditRecordRoutesSurfaceList(auditRecordRoutesSurfaceList);
        ForRecordUser forRecordUser = forRecordUserMapper.selectForRecordUserByUserId(Integer.valueOf(auditRecordRoutes.getCreateBy()));
        auditRecordRoutes.setForRecordUser(forRecordUser);
        return auditRecordRoutes;
    }

    /**
     * 查询航线审核表列表
     * 
     * @param auditRecordRoutes 航线审核表
     * @return 航线审核表
     */
    @Override
    public List<AuditRecordRoutes> selectAuditRecordRoutesList(AuditRecordRoutes auditRecordRoutes)
    {
        return auditRecordRoutesMapper.selectAuditRecordRoutesList(auditRecordRoutes);
    }

    /**
     * 新增航线审核表
     * 
     * @param auditRecordRoutes 航线审核表
     * @return 结果
     */
    @Override
    public int insertAuditRecordRoutes(AuditRecordRoutes auditRecordRoutes)
    {
        //查询航线基本信息,并且保存
        RoutesBasic routesBasic = routesBasicMapper.selectRoutesBasicById(auditRecordRoutes.getRoutesBasicId());
        AuditRecordRoutes arr = Convert.convert(AuditRecordRoutes.class, routesBasic);
        arr.setCreateBy(SecurityUtils.getUserId().toString());
        arr.setCreateTime(DateUtils.getNowDate());
        arr.setAuditState("0");
        arr.setFlightStartTime(auditRecordRoutes.getFlightStartTime());
        arr.setFlightEndTime(auditRecordRoutes.getFlightEndTime());
        arr.setDroneCount(auditRecordRoutes.getDroneIds().size());
        int count = auditRecordRoutesMapper.insertAuditRecordRoutes(arr);

        //查询面得信息
        List<RoutesSurface> routesSurfaceList = routesSurfaceMapper.selectRoutesSurfaceByBasicId(routesBasic.getId());
        routesSurfaceList.forEach(routesSurface -> {
            //保存面的信息
            AuditRecordRoutesSurface auditRecordRoutesSurface = Convert.convert(AuditRecordRoutesSurface.class, routesSurface);
            auditRecordRoutesSurface.setAuditRecordRoutesId(arr.getId());
            auditRecordRoutesSurfaceMapper.insertAuditRecordRoutesSurface(auditRecordRoutesSurface);
            //查询点位的信息,并且保存
            List<RoutesSurfacePoint> routesSurfacePointList = routesSurfacePointMapper.selectRoutesSurfacePointListByRoutesSurfaceId(routesSurface.getId());
            routesSurfacePointList.forEach(routesSurfacePoint -> {
                AuditRecordRoutesSurfacePoint auditRecordRoutesSurfacePoint = Convert.convert(AuditRecordRoutesSurfacePoint.class, routesSurfacePoint);
                auditRecordRoutesSurfacePoint.setRoutesSurfaceId(auditRecordRoutesSurface.getId());
                auditRecordRoutesSurfacePointMapper.insertAuditRecordRoutesSurfacePoint(auditRecordRoutesSurfacePoint);
            });

        });
        //设备信息表
        auditRecordRoutes.getDroneIds().forEach(droneId ->{
            AuditRecordRoutesDrone auditRecordRoutesDrone= new AuditRecordRoutesDrone();
            auditRecordRoutesDrone.setAuditRecordRoutesId(arr.getId());
            auditRecordRoutesDrone.setDroneId(droneId);
            auditRecordRoutesDroneMapper.insertAuditRecordRoutesDrone(auditRecordRoutesDrone);
        });
        return count;
    }

    /**
     * 修改航线审核表
     * 
     * @param auditRecordRoutes 航线审核表
     * @return 结果
     */
    @Override
    public int updateAuditRecordRoutes(AuditRecordRoutes auditRecordRoutes)
    {
        if("1".equals(auditRecordRoutes.getAuditState())){


            //查询航线基本信息,并且保存
            AuditRecordRoutes arr = auditRecordRoutesMapper.selectAuditRecordRoutesById(auditRecordRoutes.getId());

            ForRecordRoutes frr = Convert.convert(ForRecordRoutes.class, arr);
            frr.setAuditRecordRoutesId(arr.getId());
            forRecordRoutesMapper.insertForRecordRoutes(frr);

            arr.getAuditRecordRoutesSurfaceList().forEach(auditRecordRoutesSurface -> {
                //保存面的信息
                ForRecordRoutesSurface forRecordRoutesSurface = Convert.convert(ForRecordRoutesSurface.class, auditRecordRoutesSurface);
                forRecordRoutesSurface.setForRecordRoutesId(frr.getId());
                forRecordRoutesSurfaceMapper.insertForRecordRoutesSurface(forRecordRoutesSurface);
                //查询点位的信息,并且保存
                auditRecordRoutesSurface.getAuditRecordRoutesSurfacePointList().forEach(auditRecordRoutesSurfacePoint -> {
                    ForRecordRoutesSurfacePoint forRecordRoutesSurfacePoint = Convert.convert(ForRecordRoutesSurfacePoint.class, auditRecordRoutesSurfacePoint);
                    forRecordRoutesSurfacePoint.setRoutesSurfaceId(forRecordRoutesSurface.getId());
                    forRecordRoutesSurfacePointMapper.insertForRecordRoutesSurfacePoint(forRecordRoutesSurfacePoint);
                });
            });
            //设备信息表
            List<AuditRecordRoutesDrone> auditRecordRoutesDroneList = auditRecordRoutesDroneMapper.selectAuditRecordRoutesDroneListByArrId(arr.getId());
            auditRecordRoutesDroneList.forEach(auditRecordRoutesDrone -> {
                ForRecordRoutesDrone forRecordRoutesDrone = new ForRecordRoutesDrone();
                forRecordRoutesDrone.setDroneId(auditRecordRoutesDrone.getDroneId());
                forRecordRoutesDrone.setForRecordRoutesId(frr.getId());
                forRecordRoutesDroneMapper.insertForRecordRoutesDrone(forRecordRoutesDrone);
            });

        }
        return auditRecordRoutesMapper.updateAuditRecordRoutes(auditRecordRoutes);
    }

    /**
     * 批量删除航线审核表
     * 
     * @param ids 需要删除的航线审核表主键
     * @return 结果
     */
    @Override
    public int deleteAuditRecordRoutesByIds(Integer[] ids)
    {
        return auditRecordRoutesMapper.deleteAuditRecordRoutesByIds(ids);
    }

    /**
     * 删除航线审核表信息
     * 
     * @param id 航线审核表主键
     * @return 结果
     */
    @Override
    public int deleteAuditRecordRoutesById(Integer id)
    {
        return auditRecordRoutesMapper.deleteAuditRecordRoutesById(id);
    }
}
